﻿using System;
using System.Runtime.Serialization;

using Newtonsoft.Json;

namespace ImageProcessing.Model
{
    /// <summary>
    /// api返回值
    /// ErLin Li
    /// 2021/4/15
    /// </summary>
    [DataContract]
    public class ApiMessageEntity
    {
        /// <summary>
        /// 返回-1：身份验证失败，0：失败，1：成功， 2：异常（catch捕捉）
        /// </summary>
        [DataMember(Order = 0)]
        public ApiReturnState requstresult
        { get; set; }

        /// <summary>
        /// 如果调用失败，则显示失败原因
        /// </summary>
        [DataMember(Order = 1)]
        public string reason
        { get; set; }

        /// <summary>
        /// 操作类型（Get、Post、Put、Delete）
        /// </summary>
        [DataMember(Order = 2)]
        public ApiOperation operation
        { get; set; }

        /// <summary>
        /// 要返回的数据
        /// </summary>
        [DataMember(Order = 3)]
        public object data
        { get; set; }

        /// <summary>
        /// 返回条数
        /// </summary>
        [DataMember(Order = 4)]
        public int total
        { get; set; }

        ///// <summary>
        ///// 是否最后一条
        ///// </summary>
        //[DataMember(Order = 4)]
        //public bool isEnd
        //{ get; set; }

        public string msg { get; set; }

        /// <summary>
        /// 请求成功
        /// </summary>
        /// <param name="data">返回的数据</param>
        /// <param name="total">返回的数据条数</param>
        /// <returns></returns>
        public static ApiMessageEntity Ok(object data, int total = 0)
        {
            return new ApiMessageEntity { requstresult = ApiReturnState.Success, data = data, total = total };
        }

        public static ApiMessageEntity Ok()
            => new ApiMessageEntity { requstresult = ApiReturnState.Success };

        public static ApiMessageEntity Ok(string data)
            => new ApiMessageEntity { requstresult = ApiReturnState.Success, msg = "请求成功", data= data };

        public static ApiMessageEntity Ok(string msg, object obj)
            => new ApiMessageEntity { requstresult = ApiReturnState.Success, msg = msg, data = obj };

        /// <summary>
        /// 请求成功
        /// </summary>
        /// <param name="data">返回的数据</param>
        /// <returns></returns>
        public static ApiMessageEntity Ok(object data)
        {
            return new ApiMessageEntity { requstresult = ApiReturnState.Success, data = data, reason = null };
        }

        /// <summary>
        /// 失败
        /// </summary>
        /// <param name="str">错误信息</param>
        /// <param name="code">状态码</param>
        /// <returns></returns>
        public static ApiMessageEntity DefaultError()
        {
            return new ApiMessageEntity { requstresult = ApiReturnState.Fail, data = null, total = 0, reason = "服务出错了", msg = "服务出错了" };
        }

        /// <summary>
        /// 失败
        /// </summary>
        /// <param name="str">错误信息</param>
        /// <param name="code">状态码</param>
        /// <returns></returns>
        public static ApiMessageEntity Error(string str)
        {
            return new ApiMessageEntity { requstresult = ApiReturnState.Fail, data = null, total = 0, reason = str, msg = str };
        }

        /// <summary>
        /// 失败
        /// </summary>
        /// <param name="str">错误信息</param>
        /// <param name="code">状态码</param>
        /// <returns></returns>
        public static ApiMessageEntity Error(Exception exception)
        {
            return new ApiMessageEntity { requstresult = ApiReturnState.Fail, data = null, total = 0, reason = JsonConvert.SerializeObject(exception), msg = "调用异常" };
        }
    }

    /// <summary>
    /// 调用接口的方式
    /// </summary>
    public enum ApiOperation
    {
        /// <summary>
        /// Get
        /// </summary>
        Get = 1,

        /// <summary>
        /// Post
        /// </summary>
        Post = 2,

        /// <summary>
        /// Put
        /// </summary>
        Put = 3,

        /// <summary>
        /// Delete
        /// </summary>
        Delete = 4
    }

    /// <summary>
    /// 接口调用的返回状态
    /// </summary>
    public enum ApiReturnState
    {
        /// <summary>
        /// 身份验证失败
        /// </summary>
        AuthenticationFailure = -1,

        /// <summary>
        /// 失败
        /// </summary>
        Fail = 0,

        /// <summary>
        /// 成功
        /// </summary>
        Success = 1,

        /// <summary>
        /// 发生异常
        /// </summary>
        Exception = 2
    }
}